﻿using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace DataAchive.Helper.Converter
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 假设value是bool类型  
            if (value is bool && targetType == typeof(Visibility))
            {
                // 将bool值转换为Visibility枚举  
                return (bool)value ? Visibility.Visible : Visibility.Collapsed; // 或者使用Visibility.Hidden，取决于你的需求  
            }

            // 如果value不是bool类型或者targetType不是Visibility类型，返回默认值  
            return DependencyProperty.UnsetValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // ConvertBack方法在这个场景下通常不需要实现，因为Visibility到bool的转换可能不是双向的  
            throw new NotImplementedException();
        }
    }

    public class ComparisonBooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string selectedColumn = value as string;
            string columnToCheck = parameter as string;

            // 如果选定的列与要检查的列名称匹配，则返回 Visible，否则返回 Hidden
            return selectedColumn == columnToCheck ? Visibility.Visible : Visibility.Hidden;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class EmptyCollectionToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var collection = value as IEnumerable;
            return collection == null || !collection.Cast<object>().Any() ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class IndexToRandomColorConverter : IValueConverter
    {
        private static Random random = new Random();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int index = (int)value;
            byte[] rgb = new byte[3] {
            (byte)random.Next(200,255),
            (byte)random.Next(200,255),
            (byte)random.Next(200,255),
        };
            
            Color color = Color.FromRgb(rgb[0], rgb[1], rgb[2]);
            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class StringTruncatorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string input = value as string;
            int maxLength = 20;

            if (!string.IsNullOrEmpty(input) && input.Length > maxLength)
            {
                return input.Substring(0, maxLength) + "...";
            }

            return input;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
